package com.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.crm.base.ResponseTest;
import com.crm.domain.dto.DictDto;
import com.crm.domain.entity.DictData;
import com.crm.domain.entity.DictType;
import com.crm.domain.vo.DictTypeVO;
import com.crm.domain.vo.OneVO;
import com.crm.mapper.DictTypeMapper;
import com.crm.service.DictTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 15890
 */
@Service
@RequiredArgsConstructor
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType> implements DictTypeService {

    private final DictTypeMapper dictTypeMapper;

    @Override
    public DictTypeVO list(Integer pageNum, Integer pageSize, String dictName, String dictType, Integer status, String beginTime, String endTime) {
        Page<DictType> page = new Page<>(pageNum, pageSize);
        QueryWrapper<DictType> queryWrapper = new QueryWrapper<>();
        // 构建查询条件（如上）
        if (dictName != null && !dictName.isEmpty()) {
            queryWrapper.like("dict_name", dictName);
        }
        if (dictType != null && !dictType.isEmpty()) {
            queryWrapper.like("dict_type", dictType);
        }
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        // 处理时间范围查询（假设有对应的时间字段）
        if (beginTime != null && !beginTime.isEmpty() && endTime != null && !endTime.isEmpty()) {
            queryWrapper.between("create_time", beginTime, endTime);
        }

        // 使用 MyBatis-Plus 的 page 方法来执行分页查询
        IPage<DictType> iPage = this.page(page, queryWrapper);

        // 将查询结果转换为 DictVO 对象（你可能需要自定义这个转换过程）
        // 这里只是一个示例，具体实现取决于你的业务逻辑和 DictVO 的定义
        DictTypeVO dictVO = new DictTypeVO();

        dictVO.setTotal(iPage.getTotal());
        dictVO.setCode(200);
        dictVO.setMsg("操作成功");
        dictVO.setRows(iPage.getRecords()); // 假设 DictVO 有 records 属性来存储查询结果

        return dictVO;
    }

    /**
     * 添加字典
     *
     * @param dictDto
     * @return
     */
    @Override
    public ResponseTest add(DictDto dictDto) {
        ResponseTest responseTest = new ResponseTest();
        QueryWrapper<DictType> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_type", dictDto.getDictType());
        Long l = dictTypeMapper.selectCount(wrapper);
        if(l > 0) {
            responseTest.setCode(500);
            responseTest.setMsg("新增字典"+dictDto.getDictType()+"失败，字典类型已经存在");
            return responseTest;
        }
        DictType dictType = BeanUtil.copyProperties(dictDto, DictType.class);
        dictType.setCreateBy("admin");
        dictType.setUpdateBy("admin");
        dictType.setCreateTime(LocalDateTime.now());
        dictType.setUpdateTime(LocalDateTime.now());
        dictTypeMapper.insert(dictType);
        responseTest.setCode(200);
        responseTest.setMsg("操作成功");
        return responseTest;
    }

    /**
     *批量删除
     * @param dictId
     */
    @Override
    public void deleteAll(List<Long> dictId) {
        dictTypeMapper.All(dictId);
    }

    /**
     * 根据ID查询字典类型
     * @param id
     * @return
     */
    @Override
    public OneVO select(Long id) {
        LambdaQueryWrapper<DictType> queryWrapper = Wrappers.<DictType>lambdaQuery().eq(DictType::getDictId, id);
        DictType dictType = dictTypeMapper.selectOne(queryWrapper);
        OneVO vo = new OneVO();
        vo.setCode(200);
        vo.setMsg("操作成功");
        vo.setData(dictType);
        return vo;
    }

    /**
     * 更新
     * @param dictDto
     * @return
     */
    @Override
    public OneVO updateOne(DictDto dictDto) {


        OneVO<DictData> vo = new OneVO<>();
        QueryWrapper<DictType> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_type", dictDto.getDictType());
        Long l = dictTypeMapper.selectCount(wrapper);
        if(l > 0) {
            vo.setCode(500);
            vo.setMsg("修改"+dictDto.getDictType()+"失败，字典类型已经存在");
            return vo;
        }
        DictType dictType = BeanUtil.copyProperties(dictDto, DictType.class);
        dictType.setUpdateBy("admin");
        dictType.setUpdateTime(LocalDateTime.now());
        dictTypeMapper.updateById(dictType);
        vo.setCode(200);
        vo.setMsg("操作成功");
        return vo;
    }
}
